{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "eeMrMI0-1Dhu"
   },
   "outputs": [],
   "source": [
    "# Let's do all of our imports here, too.\n",
    "import numpy as np\n",
    "from pydrake.all import (\n",
    "    AddMultibodyPlantSceneGraph,\n",
    "    AngleAxis,\n",
    "    DiagramBuilder,\n",
    "    Integrator,\n",
    "    JacobianWrtVariable,\n",
    "    LeafSystem,\n",
    "    MeshcatVisualizer,\n",
    "    MultibodyPlant,\n",
    "    Parser,\n",
    "    PiecewisePolynomial,\n",
    "    PiecewiseQuaternionSlerp,\n",
    "    Quaternion,\n",
    "    RigidTransform,\n",
    "    RollPitchYaw,\n",
    "    RotationMatrix,\n",
    "    SceneGraph,\n",
    "    Simulator,\n",
    "    StartMeshcat,\n",
    "    TrajectorySource,\n",
    ")\n",
    "\n",
    "from manipulation.scenarios import AddMultibodyTriad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "meshcat = StartMeshcat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "QuFmomDOxoAR",
    "tags": []
   },
   "outputs": [],
   "source": [
    "builder = DiagramBuilder()\n",
    "\n",
    "plant, scene_graph = AddMultibodyPlantSceneGraph(builder, time_step=0.0)\n",
    "parser = Parser(plant, scene_graph)\n",
    "grasp = parser.AddModelsFromUrl(\n",
    "    \"package://drake/manipulation/models/wsg_50_description/sdf/schunk_wsg_50_no_tip.sdf\"\n",
    ")[0]\n",
    "# TODO(russt): Draw the pregrasp gripper, too, as transparent (drake #13970).\n",
    "# pregrasp = parser.AddModelsFromUrl(\n",
    "#    \"package://drake/manipulation/models/wsg_50_description/sdf/schunk_wsg_50_no_tip.sdf\")[0]\n",
    "brick = parser.AddModelsFromUrl(\n",
    "    \"package://drake/examples/manipulation_station/models/061_foam_brick.sdf\"\n",
    ")[0]\n",
    "AddMultibodyTriad(plant.GetFrameByName(\"body\", grasp), scene_graph)\n",
    "AddMultibodyTriad(plant.GetFrameByName(\"base_link\", brick), scene_graph)\n",
    "plant.Finalize()\n",
    "\n",
    "MeshcatVisualizer.AddToBuilder(builder, scene_graph, meshcat)\n",
    "\n",
    "diagram = builder.Build()\n",
    "context = diagram.CreateDefaultContext()\n",
    "plant_context = plant.GetMyContextFromRoot(context)\n",
    "\n",
    "# TODO(russt): Set a random pose of the object.\n",
    "\n",
    "# Get the current object, O, pose\n",
    "B_O = plant.GetBodyByName(\"base_link\", brick)\n",
    "X_WO = plant.EvalBodyPoseInWorld(plant_context, B_O)\n",
    "\n",
    "B_Ggrasp = plant.GetBodyByName(\"body\", grasp)\n",
    "p_GgraspO = [0, 0.11, 0]\n",
    "R_GgraspO = RotationMatrix.MakeXRotation(np.pi / 2.0).multiply(\n",
    "    RotationMatrix.MakeZRotation(np.pi / 2.0)\n",
    ")\n",
    "# Useful for a better image:\n",
    "p_GgraspO = [0, 0.3, 0.1]\n",
    "R_GgraspO = R_GgraspO.multiply(RotationMatrix.MakeYRotation(0.5))\n",
    "X_GgraspO = RigidTransform(R_GgraspO, p_GgraspO)\n",
    "X_OGgrasp = X_GgraspO.inverse()\n",
    "X_WGgrasp = X_WO.multiply(X_OGgrasp)\n",
    "\n",
    "print(f\"p_GO_W = {X_WGgrasp.rotation().multiply(X_GgraspO.translation())}\")\n",
    "print(f\"p_GO_G = {X_GgraspO.translation()}\")\n",
    "print(f\"p_OG_O = {X_OGgrasp.translation()}\")\n",
    "\n",
    "plant.SetFreeBodyPose(plant_context, B_Ggrasp, X_WGgrasp)\n",
    "# Open the fingers, too.\n",
    "plant.GetJointByName(\"left_finger_sliding_joint\", grasp).set_translation(\n",
    "    plant_context, -0.054\n",
    ")\n",
    "plant.GetJointByName(\"right_finger_sliding_joint\", grasp).set_translation(\n",
    "    plant_context, 0.054\n",
    ")\n",
    "\n",
    "diagram.ForcedPublish(context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "Robotic Manipulation - Basic Pick and Place.ipynb",
   "provenance": [],
   "toc_visible": true
  },
  "interpreter": {
   "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
  },
  "kernelspec": {
   "display_name": "Python 3.9.12 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
